package com.glq1218.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glq1218.comment.bo.CommentBO;
import com.glq1218.comment.bo.QueryCommentBO;
import com.glq1218.comment.entity.Comment;
import com.glq1218.comment.mapper.CommentMapper;
import com.glq1218.comment.service.CommentService;
import com.glq1218.domain.*;
import com.glq1218.user.entity.User;
import com.glq1218.user.mapper.UserMapper;
import com.glq1218.util.BeanCopyUtils;
import com.glq1218.util.ServiceUtils;
import com.glq1218.util.SortUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author glq1218
 * @description TODO
 * @since 2023/5/5 11:29
 */
@Service("commentService")
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    private final UserMapper userMapper;

    @Override
    public PaginationResult<CommentBO> queryCommentList(PaginationParam paginationParam, SortParam sortParam, IntervalStr intervalStr, QueryCommentBO queryCommentBO) {
        // 处理时间间隔
        Interval interval = ServiceUtils.getInterval(intervalStr);
        // 分页对象
        Page<Comment> commentPage = ServiceUtils.getPage(paginationParam);
        // 构建查询条件对象
        String sortSql = "";
        if (StringUtils.hasText(sortParam.getSortField())) {
            sortSql = SortUtils.getSortSql(sortParam);
        }
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper
                .like(StringUtils.hasText(queryCommentBO.getContent()), Comment::getContent, queryCommentBO.getContent())
                .eq(queryCommentBO.getStatus() != null, Comment::getStatus, queryCommentBO.getStatus())
                .eq(queryCommentBO.getArticleId() != null, Comment::getArticleId, queryCommentBO.getArticleId())
                .between(StringUtils.hasText(intervalStr.getUpdatedAtStart()) && StringUtils.hasText(intervalStr.getUpdatedAtEnd()),
                        Comment::getUpdatedAt, interval.getUpdatedAtStart(), interval.getUpdatedAtEnd())
                .between(StringUtils.hasText(intervalStr.getCreatedAtStart()) && StringUtils.hasText(intervalStr.getCreatedAtEnd()), Comment::getCreatedAt, interval.getCreatedAtStart(), interval.getCreatedAtEnd())
                .last(StringUtils.hasText(sortParam.getSortField()) && StringUtils.hasText(sortParam.getSortOrder()), sortSql);
        // 分页查询
        page(commentPage, lambdaQueryWrapper);
        // 获取总数，如果总数为0，就直接返回
        long total = commentPage.getTotal();
        if (total <= 0L) {
            return PaginationResult.<CommentBO>builder().total(total).build();
        }
        // 将Comment转为CommentBO
        commentPage.convert(comment -> {
            User user = userMapper.selectById(comment.getUserId());
            CommentBO commentBO = BeanCopyUtils.copyBean(comment, CommentBO.class);
            commentBO.setNickname(user.getNickname());
            commentBO.setUsername(user.getUsername());
            commentBO.setAvatar(user.getAvatar());
            return commentBO;
        });
        List<Comment> commentList = commentPage.getRecords();
        List<CommentBO> commentBOList = BeanCopyUtils.copyBeanList(commentList, CommentBO.class);
        return PaginationResult.<CommentBO>builder().total(total).records(commentBOList)
                .build();
    }
}
